from typing import List, Dict

class FrailtyService:
    """Fried衰弱评估业务逻辑服务"""
    
    @staticmethod
    def calculate_frailty_score(survey: dict) -> int:
        """计算衰弱评分"""
        return (
            survey.get('weightLoss', 0) + 
            survey.get('walkTime', 0) + 
            survey.get('gripStrength', 0) + 
            survey.get('physicalActivity', 0) + 
            survey.get('fatigue1', 0)
        )
    
    @staticmethod
    def get_frailty_result(score: int) -> str:
        """根据评分获取评估结果"""
        if score <= 2:
            return "正常"
        elif score <= 4:
            return "衰弱前期"
        else:
            return "衰弱"
    
    @staticmethod
    def get_frailty_statistics(surveys: List[dict]) -> Dict[str, int]:
        """获取衰弱评估统计数据"""
        total_count = len(surveys)
        if total_count == 0:
            return {
                'total': 0,
                'normal': 0,
                'pre_frail': 0,
                'frail': 0
            }
        
        normal_count = sum(1 for s in surveys if FrailtyService.calculate_frailty_score(s) <= 2)
        pre_frail_count = sum(1 for s in surveys if 3 <= FrailtyService.calculate_frailty_score(s) <= 4)
        frail_count = sum(1 for s in surveys if FrailtyService.calculate_frailty_score(s) >= 5)
        
        return {
            'total': total_count,
            'normal': normal_count,
            'pre_frail': pre_frail_count,
            'frail': frail_count
        }
    
    @staticmethod
    def get_indicator_statistics(surveys: List[dict]) -> Dict[str, Dict[str, int]]:
        """获取各项指标统计"""
        total_count = len(surveys)
        if total_count == 0:
            return {}
        
        indicators = ['weightLoss', 'walkTime', 'gripStrength', 'physicalActivity', 'fatigue1']
        indicator_names = ['体重下降', '步行时间', '握力', '体力活动', '疲劳感']
        
        statistics = {}
        for i, indicator in enumerate(indicators):
            positive_count = sum(1 for s in surveys if s.get(indicator, 0) == 1)
            statistics[indicator_names[i]] = {
                'positive': positive_count,
                'percentage': positive_count / total_count * 100 if total_count > 0 else 0
            }
        
        return statistics
    
    @staticmethod
    def get_demographic_statistics(surveys: List[dict]) -> Dict[str, any]:
        """获取人口统计学数据"""
        total_count = len(surveys)
        if total_count == 0:
            return {
                'total': 0,
                'avg_age': 0,
                'male_count': 0,
                'female_count': 0
            }
        
        # 年龄统计
        ages = [s.get('age', 0) for s in surveys if s.get('age')]
        avg_age = sum(ages) / len(ages) if ages else 0
        
        # 性别统计
        male_count = sum(1 for s in surveys if s.get('gender') == '男')
        female_count = sum(1 for s in surveys if s.get('gender') == '女')
        
        return {
            'total': total_count,
            'avg_age': avg_age,
            'male_count': male_count,
            'female_count': female_count,
            'male_percentage': male_count / total_count * 100 if total_count > 0 else 0,
            'female_percentage': female_count / total_count * 100 if total_count > 0 else 0
        }